<template>
  <el-table
    :data="PermissionArr"
    style="margin-bottom: 20px"
    row-key="id"
    border
    default-expand-all
  >
    <el-table-column prop="name" label="名称"></el-table-column>
    <el-table-column prop="code" label="权限值"></el-table-column>
    <el-table-column prop="updateTime" label="修改时间"></el-table-column>
    <el-table-column label="操作">
      <!-- 插槽的结构写法 前为数据对象 后为数据序号 -->
      <!-- row:即为已有的菜单对象|按钮对象的数据 -->
      <template #="{ row, $index }">
        <el-button
          type="primary"
          size="small"
          icon="Plus"
          :disabled="row.level == 4 ? true : false"
          @click="addPermission(row)"
        >
          {{ row.level == 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button
          type="primary"
          size="small"
          icon="Edit"
          :disabled="row.level == 1 ? true : false"
          @click="updatePermission(row)"
        >
          编辑
        </el-button>
        <el-popconfirm
          :title="`确认删除${row.name}吗?`"
          @confirm="deleteMenu(row.id)"
        >
          <template #reference>
            <el-button
              type="primary"
              size="small"
              icon="Delete"
              :disabled="row.level == 1 ? true : false"
            >
              删除
            </el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 对话框组件:添加|更新 已有组件的结构 -->
  <el-dialog
    v-model="dialogVisable"
    :title="`${menuData.id ? '更新菜单' : '添加菜单'}`"
  >
    <!-- 表单组件 -->
    <el-form label-width="100px">
      <el-form-item label="名称">
        <el-input
          placeholder="请输入菜单名称"
          v-model="menuData.name"
        ></el-input>
      </el-form-item>
      <el-form-item label="权限值">
        <el-input placeholder="请输入权限值" v-model="menuData.code"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisable = false">取消</el-button>
        <el-button @click="save">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
// 引入请求数据的函数
import {
  reqAllPermission,
  reqRemmoveMenu,
  reqAddOrUpdateMenu,
} from '@/api/acl/menu'
//存储菜单的数据类型
let PermissionArr = ref<PermissionList>([])
// 引入全部菜单的请求的返回值类型
import type {
  MenuParams,
  PermissionResponseData,
  PermissionList,
  Permission,
} from '@/api/acl/menu/type'
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'

// 控制对话框的显示与隐藏
let dialogVisable = ref<boolean>(false)
// 新增|更新的菜单对象
let menuData = reactive<MenuParams>({
  code: '', //权限值
  level: 0, //几级菜单
  name: '', //菜单的名字
  pid: 0,
})

// 获取已有菜单的数据的函数
const getHasPermission = async () => {
  let result: PermissionResponseData = await reqAllPermission()
  if (result.code == 200) {
    PermissionArr.value = result.data
  }
}

// 添加菜单的回调
const addPermission = (row: Permission) => {
  // 清除存储的数据
  Object.assign(0, {
    id: 0,
    code: '', //权限值
    level: 0, //几级菜单
    name: '', //菜单的名字
    pid: 0,
  })
  // 显示对话框
  dialogVisable.value = true
  // 收集新增菜单的level数据
  menuData.level = row.level + 1
  // 给谁添加的子菜单
  menuData.pid = row.id as number
  console.log(menuData)
}
// 更新菜单的回调
const updatePermission = (row: Permission) => {
  // 数据合并
  Object.assign(menuData, row)
  // 显示对话框
  dialogVisable.value = true
}
// 对话框的确认按钮的回调函数
const save = async () => {
  let result: any = await reqAddOrUpdateMenu(menuData)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: menuData.id ? '更新成功' : '添加成功',
    })
    getHasPermission()
  } else {
    ElMessage({
      type: 'success',
      message: menuData.id ? '更新失败' : '添加失败',
    })
  }

  // 显示对话框
  dialogVisable.value = false
}
// 删除按钮的回调
const deleteMenu = async (menuId: number) => {
  let result: any = await reqRemmoveMenu(menuId)
  console.log(result)
  if (result.code == 200) {
    ElMessage({ type: 'success', message: '删除成功' })

    getHasPermission()
  } else {
    ElMessage({ type: 'error', message: '删除失败' })
  }
}

// 组件挂载完毕后请求数据
onMounted(() => {
  getHasPermission()
})
</script>
<style lang="scss" scoped></style>
